﻿cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
project(VisualCompass LANGUAGES CXX C)

#加载自定义模块
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} " ${PROJECT_SOURCE_DIR}/../../build/cmake")
message(${CMAKE_MODULE_PATH})
#设置库文件路径
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../lib)
#设置可执行程序路径
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../bin)
#设置子模块
set(SUB_DIR_LIST
        "."
        "visual_compass"
        )#TODO


set(INSTALL_PATH_LIB ${CMAKE_INSTALL_PREFIX}/lib)
set(INSTALL_PATH_INCLUDE ${CMAKE_INSTALL_PREFIX}/include)
configure_file(./include/config.h.in ./include/config.h)
configure_file(cmake/packge.cmake.in ./cmake/${CMAKE_PROJECT_NAME}.cmake)
foreach(SUB_DIR ${SUB_DIR_LIST})
    #遍历源文件
    aux_source_directory(src/${SUB_DIR} SRC_LIST)
endforeach()
set(LINK_LIB_LIST)

#opencv
find_package(OpenCV 3.3 REQUIRED)
message(STATUS "找到 OpenCV 库:\"${OpenCV_DIR}\"")
list(APPEND  LINK_LIB_LIST ${OpenCV_LIBS})

#打印库文件
message(STATUS "将链接依赖库:${LINK_LIB_LIST}")
#引用头文件路径
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/../../build/include)
link_directories(${PROJECT_SOURCE_DIR}/../../build/lib)

# Set the build postfix extension according to what configuration is being built.
IF (CMAKE_BUILD_TYPE MATCHES "Release")
    SET(CMAKE_BUILD_POSTFIX "${CMAKE_RELEASE_POSTFIX}")
ELSEIF (CMAKE_BUILD_TYPE MATCHES "MinSizeRel")
    SET(CMAKE_BUILD_POSTFIX "${CMAKE_MINSIZEREL_POSTFIX}")
ELSEIF(CMAKE_BUILD_TYPE MATCHES "RelWithDebInfo")
    SET(CMAKE_BUILD_POSTFIX "${CMAKE_RELWITHDEBINFO_POSTFIX}")
ELSEIF(CMAKE_BUILD_TYPE MATCHES "Debug" )
    SET(CMAKE_BUILD_POSTFIX "${CMAKE_DEBUG_POSTFIX}")
ELSE()
    SET(CMAKE_BUILD_POSTFIX "")
ENDIF()

#使能c++14
add_compile_options(-std=c++14 )

add_library(${CMAKE_PROJECT_NAME}_shared SHARED ${SRC_LIST})
target_link_libraries(${CMAKE_PROJECT_NAME}_shared ${LINK_LIB_LIST})
set_target_properties(${CMAKE_PROJECT_NAME}_shared PROPERTIES OUTPUT_NAME "${CMAKE_PROJECT_NAME}")

#编译静态库
add_library(${CMAKE_PROJECT_NAME}_static STATIC ${SRC_LIST})
set_target_properties(${CMAKE_PROJECT_NAME}_static PROPERTIES OUTPUT_NAME "${CMAKE_PROJECT_NAME}")
#安装静态库至系统目录
install(TARGETS ${CMAKE_PROJECT_NAME}_shared  ARCHIVE DESTINATION ${INSTALL_PATH_LIB} LIBRARY DESTINATION ${INSTALL_PATH_LIB})
install(TARGETS ${CMAKE_PROJECT_NAME}_static ARCHIVE DESTINATION ${INSTALL_PATH_LIB})
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/    DESTINATION ${CMAKE_INSTALL_PREFIX}/include FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp")
install(DIRECTORY ${PROJECT_SOURCE_DIR}/doc/        DESTINATION ${CMAKE_INSTALL_PREFIX}/doc FILES_MATCHING PATTERN "*")
install(DIRECTORY ${PROJECT_SOURCE_DIR}/bash/       DESTINATION ${CMAKE_INSTALL_PREFIX}/script FILES_MATCHING PATTERN "*.bash" PATTERN "*.sh")
install(DIRECTORY ${PROJECT_SOURCE_DIR}/python/     DESTINATION ${CMAKE_INSTALL_PREFIX}/python FILES_MATCHING PATTERN "*")
install(DIRECTORY ${PROJECT_SOURCE_DIR}/cmake/      DESTINATION ${CMAKE_INSTALL_PREFIX}/cmake FILES_MATCHING PATTERN "*.cmake")
install(DIRECTORY ${PROJECT_BINARY_DIR}/cmake/      DESTINATION ${CMAKE_INSTALL_PREFIX}/cmake FILES_MATCHING PATTERN "*.cmake")
install(DIRECTORY ${PROJECT_SOURCE_DIR}/resource/   DESTINATION ${CMAKE_INSTALL_PREFIX}/resource FILES_MATCHING PATTERN "*")
install(DIRECTORY ${PROJECT_SOURCE_DIR}/example/    DESTINATION ${CMAKE_INSTALL_PREFIX}/example/${CMAKE_PROJECT_NAME} FILES_MATCHING PATTERN "*.cpp")

#测试程序
add_subdirectory(example)
include("unittest/CMakeLists.txt")
